<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout :: common_header(~{::title})">
    <title th:text="${shop.name+'退货订单-退货管理-OMS订单管理系统'}"></title>
</head>
<body th:replace="layout :: common_body(~{::div.layui-body})">
<div class="layui-body" style="padding:15px;">
    <style type="text/css">
        .T-box {
            width: 400px;
            padding: 0 20px;
            text-align: left;
        }

        .T-box p {
            position: relative;
            height: 36px;
            line-height: 36px;
            font-size: 14px;
            margin-bottom: 15px;
        }

        .T-box p span {
            display: inline-block;
            padding-right: 15px;
            text-align: right;
            width: 110px;
            line-height: 36px;
            font-size: 14px;
        }

        .T-box p input, .T-box p select {
            width: 160px;
            padding: 0 5px;
            height: 34px;
            line-height: 34px;
            font-size: 14px;
            border: 1px solid #d3d3d3;
            outline: none;
        }

        .T-box p i {
            padding: 0 10px;
            line-height: 36px;
            font-style: normal;
        }

        .T-box p b {
            display: inline-block;
            margin-left: 10px;
            cursor: pointer;
            width: 60px;
            height: 36px;
            line-height: 36px;
            background: #c33;
            color: #fff;
            text-align: center;
            font-size: 13px;
            font-weight: normal;
        }

        .T-result {
            margin-bottom: 0;
        }

        .T-result em {
            color: #c33;
            font-style: normal;
        }

        .update {
            display: inline-block;
            margin-left: 15px;
            padding: 0 20px;
            height: 40px;
            line-height: 40px;
            font-size: 14px;
            background: #009789;
            color: #fff;
            text-align: center;
            border-radius: 3px;
            cursor: pointer;
        }

        .update i {
            display: inline-block;
            margin-right: 10px;
            vertical-align: middle;
            width: 30px;
            height: 30px;
            background: url();
        }

    </style>
    <div class="layui-tab layui-tab-brief" lay-filter="demo">
        <ul class="layui-tab-title">
            <li ><a th:href="@{/tao/dashboard(shopId=${shopId})}">首页</a></li>
            <li><a th:href="@{/tao/order_list(shopId=${shopId})}">订单管理</a></li>
            <!-- <li><a th:href="@{/tao/order_create(shopId=${shopId})}">手动创建订单</a></li> -->
            <li class="layui-this"><a th:href="@{/tao/refund_list(shopId=${shopId})}">退货管理</a></li>
            <!-- <li><a th:href="@{/tao/goods_list(shopId=${shopId})}">商品管理</a></li> -->
            <li ><a th:href="@{/tao/goods_traffic_list(shopId=${shopId})}">商品流量管理</a></li>
            <li ><a th:href="@{/tao/goods_optimization_record(shopId=${shopId})}">商品优化记录</a></li>
        </ul>
    </div>
    <!-- content starts -->
    <!--    <blockquote class="layui-elem-quote">售后订单</blockquote>-->

    <fieldset class="layui-elem-field">
        <legend>按条件搜索</legend>
        <div class="layui-field-box">
            <form class="layui-form layui-form-pane1" method="get" action="/tao/refund_list">
                <input type="hidden" name="shopId" th:value="${shop.id}"/>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" name="returnOrderNum" placeholder="退款编号" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <input type="text" name="order_num" placeholder="订单编号" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <input type="text" name="logisticsCode" placeholder="退货物流单号" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">申请时间</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="startTime" id="startTime" autocomplete="off"
                                   placeholder="yyyy-MM-dd">
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="endTime" id="endTime" autocomplete="off" placeholder="yyyy-MM-dd">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="state">
                                <option value="">售后状态</option>
                                <option th:each="item:${T(com.b2c.entity.enums.tao.EnumDcTmallOrderReturnStatus).values()}" th:value="${item.index}" th:text="${item.name}">退款待审核</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn">搜索</button>
                        <i>总共：<span style="color: red" th:text="${totalSize}"></span>条记录</i>
                    </div>
                    <div class="layui-inline">
                        <span id="upd_order" class="layui-btn layui-btn-normal">更新Tao退款订单</span>
                        <span id="upd_order_" style="display: none;" class="update"><i></i>更新中</span>
                    </div>
                </div>

            </form>
        </div>
    </fieldset>

    <!-- start dd-wrapper -->
    <link rel="stylesheet" href="/goods/order.css"/>
    <div class="dd-wrapper">
        <table class="dd-table">
            <tr>
                <th>ID</th>
                <th>退款单号</th>
                <th>退款金额</th>
                <th>订单号</th>
                <th>商品图片</th>
                <th>商品名</th>
                <th>规格</th>
                <th>是否需要退货</th>
                <th>退款原因</th>
                <th>退货物流</th>
                <th>售后状态</th>
                <th>创建时间</th>
                <th>ERP状态</th>
                <th>操作</th>
            </tr>
            <tr th:each="item:${list}">
                <td th:text="${item.id}"></td>
                <td th:text="${item.refundId}"></td>
                <td th:text="${item.refundFee}"></td>
                <td th:text="${item.orderId}"></td>
                <td>
                    <img th:src="${item.goodsImg}" style="width: 50px;height:50px;" />
                </td>
                <td th:text="${item.goodsName}"></td>
                <td th:text="${item.spec}"></td>
                <td>是</td>
                <td>
                    <span th:text="${item.reason}" ></span><br/>
                    <span th:text="${item.desc}" ></span>
                </td>
                <td>
                    <span th:text="${item.logisticsCompany}" ></span><br/>
                    <span th:text="${item.logisticsCode}" ></span>
                    <span class="layui-btn layui-btn-xs layui-btn-primary hand_express" th:attr="data-id=${item.id}">修改物流</span>
                </td>
                <td>
                    <span th:text="${T(com.b2c.entity.enums.tao.EnumDcTmallOrderReturnStatus).getName(item.status)}"></span>
                </td>
                <td>
                    <p th:text="${#dates.format(item.createOn * 1000, 'yyyy-MM-dd HH:mm:ss')}"></p>
                </td>
                <td>
                    <span th:if="${item.auditStatus==0}" style="color: rgb(21, 205, 222);">未处理</span>
                    <span th:if="${item.auditStatus==2}" style="color: rgb(37, 204, 45);">已签收待处理</span>
                    <span th:if="${item.auditStatus==9}" style="color: red;">供应商已退款</span>
                    <span th:if="${item.auditStatus==99}" style="color: rgba(90, 90, 90, 0.771);">已关闭</span>
                    <span th:if="${item.auditStatus==91}" style="color: rgba(90, 90, 90, 0.771);">等待财务处理</span>
                    <span th:if="${item.auditStatus==100}" style="color: rgba(90, 90, 90, 0.771);">计入损失</span>
                    <a title="修改状态" class="edit_status"  th:attr="data-id=${item.getId()}" href="javascript:;">
                        <i class="layui-icon layui-icon-edit"></i>
                    </a>
                    <p th:text="${item.auditTime}"></p>
                </td>
                <td class="center">
                    <a th:href="@{/tao_order/refund_detail(refId=${item.refundId},shopId=${shop.id})}">查看详情</a>
                    <!-- <a class="track confirm_refund" href="javascript:;">确认退货</a> -->
                    <!-- <a href="javascript:;" class="updateRefundByNum" th:attr="data-id=${item.refundId}">更新</a> -->
                    <br/>
                    <a th:if="${item.auditStatus !=99  }" th:attr="refund-id=${item.refundId},describe=${item.desc},shopId=${item.shopId},trackcode=${item.logisticsCode}" href="javascript:;" title="加入代办列表" class="addtodo">
                        <i class="layui-icon layui-icon-log"></i>
                    </a>
                    <a th:if="${item.auditStatus==0  }" class="layui-btn-sm layui-btn-primary layui-btn-radius sign_refund"  th:attr="data-id=${item.getId()},auditStatus=2" href="javascript:;">签收</a>
                    <a th:if="${item.auditStatus==2  }" class="sign_refund"  th:attr="data-id=${item.getId()},auditStatus=9" title="标记为已退款" href="javascript:;">
                        <i class="layui-icon layui-icon-rmb"></i>
                    </a>
                </td>
            </tr>
            <tr th:if="${not_result != null && not_result == 1 }">
                <td colspan="10">
                    没有找到退款单 <span th:text="${returnOrderNum}"></span>，<a href="javascript:;" class="updateRefundByNum" th:attr="data-id=${returnOrderNum}">点击更新</a>
                </td>
            </tr>
        </table>
    </div>
    <!-- end dd-wrapper -->
    <div class="layui-box layui-laypage layui-laypage-default pageBox">
        <paging:pager th:value="${pageIndex}" th:rows="${totalSize}" th:size="${pageSize}"/>
    </div>
    <script type="text/template" id="edit_status">
        <div class="T-box">
            <p><span>状态</span>
                <select id="new-status">
                    <option value="0">请选择状态</option>
                    <option value="10">换货已完成</option>
                    <option value="99" selected>已关闭</option>
                    <option value="91" selected>等待财务处理</option>
                    <option value="100" selected>计入损失</option>
                </select>
            </p>
            <p><span>备注</span>
                <input type="text" autocomplete="off" value="" name="new-remark"/>
            </p>
            <p class="T-result"><span>&nbsp;</span><em id="erro"></em></p>
        </div>
    </script>
    <script type="text/template" id="tuikuan">
        <div class="T-box">
            <p><span>订单编号</span><i id="ddbh"></i></p>
            <p><span>退货编号</span><i id="thbh"></i></p>
            <p><span>付款时间</span><i id="fksj"></i></p>
            <p><span>退款时间</span><i id="tksj"></i></p>
            <p><span>订单总额</span><i id="ddze"></i></p>
            <p><span>收款人</span><i id="skr"></i></p>
            <p><span>退款金额</span><i id="skje"></i></p>
            <p><span>退款方式</span><i>按原账户退回</i></p>
        </div>
    </script>
    <script type="text/template" id="send-goods">
        <div class="T-box">
            <p><span>快递公司</span>
                <select id="send-company">
                    <option value="">请选择快递公司</option>
                    <option th:each="item:${company}" th:value="${item.getCode()}" th:text="${item.getName()}"></option>
                </select>
            </p>
            <p><span>快递单号</span><input type="text" name="express_code"/></p>
            <p><span>发件人地址</span><input type="text" name="express_address"/></p>
            <p class="T-result"><span>&nbsp;</span><em id="erro"></em></p>
        </div>
    </script>

    <script src="/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="/goods/Tips/Tips.min.js"></script>
    <script type="text/javascript">
        layui.use(['laydate', 'form'], function () {
            var laydate = layui.laydate;
            var form = layui.form;
            laydate.render({
                elem: '#startTime'
            });
            laydate.render({
                elem: '#endTime'
            });
        });
        $(function () {

            

            // 填写物流
            $('.hand_express').click(function (e) {
                e.stopPropagation();
                e.preventDefault();
                var id = $(this).attr('data-id');
                Tips.confirm({
                    title: '手动填写物流信息',
                    content: $('#send-goods').html(),
                    define: function () {
                        var data = {
                            id: id,
                            company: $("#send-company  option:selected").text(),
                            companyCode: $("#send-company  option:selected").val(),
                            code: $("input[name='express_code']").val(),
                            address: $("input[name='express_address']").val()
                        };
                        $.ajax({
                            url: "/ajax_tao/edit_refund_logistics_code",
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(data),
                            success: function (res) {
                                if (res.code == 0) {
                                    Tips.alert('成功', function () {
                                        location.reload(true);
                                    });
                                } else {
                                    Tips.alert(res.msg);
                                }
                            }
                        });
                    }
                });
            });
            //加入代办
            $(".addtodo").click(function(){
                var refundId = $(this).attr('refund-id');
                var shopId = $(this).attr('shopId');
                var describe = $(this).attr('describe');
                if(describe == ''||describe==null) describe = "退货";
                var trackcode = $(this).attr('trackcode');
                if(trackcode == undefined) trackcode='';
                else trackcode += "";

                var obj = $(this).siblings('b').eq(0);

                layer.prompt({
                    formType: 2,
                    value: '淘宝'+describe+'：'+trackcode,
                    title: '代办内容'
                }, function(value, index, elem){
                    var data = {sourceType:1,sourceId:refundId,shopId:shopId, content:value};
                    $.ajax({
                        url: "/ajax_todo/add_todo",
                        type: "POST",
                        dataType: "JSON",
                        contentType: 'application/json',
                        data:JSON.stringify(data),
                        success: function (res) {

                            if(res.code == 0){

                                $(obj).text(value);
                                layer.close(index)
                                layer.msg(res.msg);
                                // location=location;
                            }else
                                layer.alert(res.msg);
                        }
                    });


                });
            });
            //修改状态edit_status
            $('.edit_status').click(function (e) {
                e.stopPropagation();
                e.preventDefault();
                var id = $(this).attr('data-id');
                Tips.confirm({
                    title: '修改处理状态',
                    content: $('#edit_status').html(),
                    define: function () {
                        var data = {
                            id: id,
                            auditStatus: $("#new-status  option:selected").val(),
                            remark: $("input[name='new-remark']").val()
                        };
                        $.ajax({
                            url: "/ajax_tao/sign_refund",
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(data),
                            success: function (res) {
                                if (res.code == 0) {
                                    Tips.alert('成功', function () {
                                        location.reload(true);
                                    });
                                } else {
                                    Tips.alert(res.msg);
                                }
                            }
                        });
                    }
                });
            });
            //标记为已处理
            $(".sign_refund").click(function () {
                var $this = $(this);
                let refId = $(this).attr('data-id');
                let auditStatus = $(this).attr('auditStatus');

                if(confirm("该操作不可逆，是否确定要标记为已处理？")){
                    var data = {
                        id: refId,
                        auditStatus:auditStatus
                    };
                    $.ajax({
                        url: "/ajax_tao/sign_refund",
                        type: "POST",
                        dataType: 'json',
                        contentType: 'application/json',
                        data: JSON.stringify(data),
                        success: function (res) {
                            if (res.code == 0) {
                               layer.msg("处理成功");
                                $this.parent().parent().remove();
                               // window.location.reload();
                            } else {
                                alert(res.msg);
                                return false;
                            }
                        }
                    });
                }

            })



            //更新订单根据退款单号
            $(".updateRefundByNum").click(function () {
                var refundId = $(this).attr("data-id");
                console.log("更新"+refundId);
                $.ajax({
                    url: "/tao_ajax/pull_refund_order_by_num",
                    type: "POST",
                    dataType: "JSON",
                    contentType: 'application/json',
                    data:JSON.stringify({shopId:[[${shop.id}]],orderId:refundId}),
                    success: function (res) {
                        if (res.code == 0) {
                            Tips.alert({
                                title: '更新订单',
                                content: '【' + res.msg + '】',
                                define: function () {
                                    window.location.reload();
                                }
                            });

                        } else if (res.code == 601) {
                            // $("#upd_order").show();
                            // $("#upd_order_").hide();
                            alert(res.msg);
                            // Tips.alert(res.msg);
                            location.href = "/tao_oauth?shopId=[[${shop.id}]]";
                        } else {
                            Tips.alert(res.msg);
                        }
                    }
                })
            })
            //更新订单
            $("#upd_order").click(function () {
                $("#upd_order").hide();
                $("#upd_order_").show();
                $.ajax({
                    url: "/tao_ajax/pull_refund_order",
                    type: "POST",
                    dataType: "JSON",
                    contentType: 'application/json',
                    data:JSON.stringify({shopId:[[${shop.id}]]}),
                    success: function (res) {
                        if (res.code == 0) {
                            Tips.alert({
                                title: '更新订单',
                                content: '更新成功：【' + res.msg + '】',
                                define: function () {
                                    location.href = "/tao_order/refund_list?shopId=[[${shop.id}]]";
                                }
                            });
                        } else if (res.code == 601) {
                            // $("#upd_order").show();
                            // $("#upd_order_").hide();
                            alert(res.msg);
                            // Tips.alert(res.msg);
                            location.href = "/tao_oauth?shopId=[[${shop.id}]]";
                        } else {

                            Tips.alert(res.msg);
                        }
                        $("#upd_order").show();
                        $("#upd_order_").hide();
                    }
                })
            });


            //AJAX 退款
            $(".confirm_refund").click(function () {
                var id = $(this).attr("id");
                if(confirm("确认要处理退款订单吗？")){
                    var data = {
                        id:id
                    }
                    $.ajax({
                        url: "/ajax_tao/reviewRefund",
                        type: "POST",
                        dataType: 'json',
                        contentType: 'application/json',
                        data: JSON.stringify(data),
                        success: function (res) {
                            if (res.code == 0) {
                                Tips.alert('成功', function () {
                                    location.reload(true);
                                });
                            } else {
                                alert(res.msg);
                                return false;
                            }
                        }
                    });
                }
            })
        });
    </script>
</div>
</body>
</html>
